 class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

 class Solution {
     public ListNode removeNthFromEnd(ListNode head, int n) {
         //先求出链表的长度
         if(head == null || head.next == null){
             return null;
         }
         int length = 1;
         ListNode prev = null;
         ListNode cur = head;
         while(cur.next != null){
             cur = cur.next;
             length ++;//链表长度
         }
         //cur节点要往后移动num次

         int num = length - n;
         if(num == 0){
             head = head.next;
             return head;
         }
         cur = head;
         while(num != 0){
             prev = cur;
             cur = cur.next;
             num --;
         }
         prev.next = cur.next;

         return head;
     }
 }